using System;
using Gtk;

public partial class MainWindow : Gtk.Window
{
	public MainWindow () : base(Gtk.WindowType.Toplevel)
	{Build ();}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{Salir();}
	
	protected virtual void OnNuevoActionActivated (object sender, System.EventArgs e)
	{Nuevo();}
	
	protected virtual void OnAbrirActivated (object sender, System.EventArgs e)
	{AbrirFichero();}
	
	protected virtual void OnGuardarActivated (object sender, System.EventArgs e)
	{/*GuardarFichero();*/}
	
	protected virtual void OnGuardarComoActionActivated (object sender, System.EventArgs e)
	{GuardarComo();}
	
	protected virtual void OnSalirActionActivated (object sender, System.EventArgs e)
	{Salir();}
	
	protected void OnCerrarPestaaActionActivated (object sender, System.EventArgs e)
	{CerrarPestaña();}
	
	/*------------- AQUI LOS MÉTODOS A UTILIZAR Y LA INICIALIZACION DEL STRING -------------*/
	
	int count=0;
	
	public void Nuevo(){
		NuevaPestaña("Pestaña"+count,"");
		count = count + 1;
	}
	
	public void NuevaPestaña(string name,string content){
		MiTextView textView = new MiTextView();
		textView.Visible = true;
		notebook.AppendPage(textView, new Label(name));
		textView.Buffer.Text = content ;
	}
	
	public void Limpiar(){
		//MuestraTexto.Buffer.Text = "";
	}

	public string AbrirFichero(){
		FileChooserDialog fileChooserDialog = new FileChooserDialog("Abrir",
		                            this,
		                            FileChooserAction.Open,
		                            "Cancelar",ResponseType.Cancel,
		                            "Abrir",ResponseType.Accept);
		Gtk.FileFilter fileFilter = new Gtk.FileFilter(); 
        fileFilter.Name="Format files"; 
        fileFilter.AddPattern("*.txt");
		fileFilter.AddPattern("*.html"); 
        fileChooserDialog.AddFilter(fileFilter); 
		
		if (fileChooserDialog.Run() == (int)ResponseType.Accept){
			System.IO.StreamReader streamReader = new System.IO.StreamReader(fileChooserDialog.Filename);
			NuevaPestaña(fileChooserDialog.Filename,streamReader.ReadToEnd());
			streamReader.Close();
		}
		
		string rF = fileChooserDialog.Filename;
		fileChooserDialog.Destroy();
		return rF; 
	}
	
	public void GuardarFichero(string rF, string content){
		if(string.IsNullOrEmpty(rF)){
			GuardarComo();
		}else{
			System.IO.StreamWriter sw = new System.IO.StreamWriter(rF);
			sw.Write(content);
			sw.Close();
		}
	}
	
	public string GuardarComo(){
		FileChooserDialog fileChooserDialog = new FileChooserDialog("Guardar como ...",
		                            this,
		                            FileChooserAction.Save,
		                            "Cancelar",ResponseType.Cancel,
		                            "Guardar",ResponseType.Accept);
		Gtk.FileFilter fileFilter = new Gtk.FileFilter(); 
        fileFilter.Name="Format files"; 
        fileFilter.AddPattern("*.txt");
		fileFilter.AddPattern("*.html"); 
        fileChooserDialog.AddFilter(fileFilter); 
		
		if ((ResponseType)fileChooserDialog.Run() == ResponseType.Accept){
			string fileName = fileChooserDialog.Filename;
			if (!fileName.EndsWith(".txt", StringComparison.CurrentCultureIgnoreCase))
				fileName = fileName + ".txt";
			//GuardarFichero(fileName,MuestraTexto.Buffer.Text);
		}
		string rF = fileChooserDialog.Filename;
		fileChooserDialog.Destroy();
		return rF;
	}
	
	public void CerrarPestaña(){
		notebook.RemovePage(notebook.Page);
		count = count - 1;
	}
	
	public void Salir(){
		Application.Quit();
	}
	
	
	
}

